<template>
  {{ elementX }},{{ elementY }},{{ isOutside }}
  <div class="goods-image">
    <!-- 左侧大图-->
    <div class="leftImg" ref="target">
      <img :src="imageList[activeIndex]" />
      <!-- 蒙层小滑块 -->
      <!-- 动态定位的 div -->
      <div
        class="layer"
        ref="layer"
        v-show="!isOutside"
        :style="{ left: `${left}px`, top: `${top}px` }"
      ></div>
    </div>
    <!-- 小图列表 -->
    <ul class="smallImg">
      <li
        v-for="(item, index) in imageList"
        :key="index"
        @mouseenter="enterHander(index)"
        :class="index === activeIndex ? 'active' : ''"
      >
        <img :src="item" />
      </li>
    </ul>
    <!-- 放大镜大图 -->
    <!-- backgroundPositionX: 0px 和 `backgroundPositionY: `0px：
            设置背景图像的位置，水平和垂直方向都为 0px
     -->
    <div
      class="largeImg"
      v-show="!isOutside"
      :style="[
        {
          backgroundImage: `url(${imageList[activeIndex]})`,
          backgroundPositionX: `${positionX}px`,
          backgroundPositionY: `${positionY}px`,
        },
      ]"
    ></div>
  </div>
</template>

<script setup lang="ts">
import { defineOptions, ref, onMounted, watch } from "vue";
import { useMouseInElement } from "@vueuse/core";
defineOptions({
  name: "ImageView",
});
//props适配imageList列表
defineProps({
  imageList: {
    type: Array,
    default: () => [],
  },
});
// 图片列表
// const imageList = [
//   "https://yanxuan-item.nosdn.127.net/d917c92e663c5ed0bb577c7ded73e4ec.png",
//   "https://yanxuan-item.nosdn.127.net/e801b9572f0b0c02a52952b01adab967.jpg",
//   "https://yanxuan-item.nosdn.127.net/b52c447ad472d51adbdde1a83f550ac2.jpg",
//   "https://yanxuan-item.nosdn.127.net/f93243224dc37674dfca5874fe089c60.jpg",
//   "https://yanxuan-item.nosdn.127.net/f881cfe7de9a576aaeea6ee0d1d24823.jpg",
// ];
// 1.实现鼠标移入交互
const activeIndex = ref(0);
//获取左侧大图的实例
let target = ref(null);
//获取遮罩层实例
let layer = ref(null);
// 定义响应式遮罩层数据来存储宽度和高度
const layerWidth = ref(0);
const layerHeight = ref(0);
//初始化遮罩层left/top变量
const left = ref(0);
const top = ref(0);
//初始化放大镜的x/y变量
let positionX = ref(0);
let positionY = ref(0);
onMounted(() => {
  layerWidth.value = layer.value.offsetWidth;
  layerHeight.value = layer.value.offsetHeight;
});
// 2.获取鼠标的相对位置
const { elementX, elementY, isOutside } = useMouseInElement(target);
//鼠标进入 左侧图动态切换
const enterHander = (index) => {
  //   console.log(index);
  activeIndex.value = index;
};
//3.控制遮罩层跟随鼠标移动(监听elementX/Y变化，重新设置left/top值)
watch([elementX, elementY, isOutside], () => {
  // console.log("xy变化了");
  if (isOutside.value) return;
  // console.log("后续逻辑执行力");
  //有效范围内控制滑块距离
  //横向
  if (elementX.value > 100 && elementX.value < 300) {
    left.value = elementX.value - layerWidth.value / 2;
  }
  //纵向
  if (elementY.value > 100 && elementY.value < 300) {
    top.value = elementY.value - layerHeight.value / 2;
  }
  //处理边界
  if (elementX.value > 300) {
    left.value = 200;
  }
  if (elementX.value < 100) {
    left.value = 0;
  }
  if (elementY.value > 300) {
    top.value = 200;
  }
  if (elementY.value < 100) {
    top.value = 0;
  }
  // 控制大图的显示
  positionX.value = -left.value * 2;
  positionY.value = -top.value * 2;
});
</script>

<style scoped lang="scss">
.goods-image {
  position: relative;
  display: flex;
  width: 480px;
  height: 400px;
  // 左侧大图
  .leftImg {
    width: 400px;
    height: 400px;
    background-color: #f5f5f5;
  }
  // 遮罩层
  .layer {
    // 绝对定位 然后跟随鼠标控制left和top属性就可以让滑块移动起来
    position: absolute;
    left: 0;
    top: 0;
    width: 200px;
    height: 200px;
    background: rgba(0, 0, 0, 0.2);
  }
  //小图列表
  .smallImg {
    width: 80px;
    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;
      &:hover,
      &.active {
        border: 2px solid $xtxColor;
      }
    }
  }
  //放大镜大图
  .largeImg {
    position: absolute;
    top: 0;
    left: 412px;
    width: 400px;
    height: 400px;
    z-index: 500;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    // 背景图:盒子的大小 = 2:1  将来控制背景图的移动来实现放大的效果查看 background-position
    background-size: 800px 800px;
    background-color: #f8f8f8;
  }
}
</style>
